/*
 *  Class name: DialogChooserManagedBean.java
 *  Version: 1.0
 *  Date: 28.2.2014
 */
package cz.muni.fi.pa165.soccergamesarchive.managedbeans.application;

import java.io.Serializable;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

/**
 *
 * @author Michal Ďuriš
 */
@Named(value = "dialogChooserManagedBean")
@ViewScoped
public class DialogChooserManagedBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private int currentPage = 1;

    private DialogChooserListenable resultListener;

    private String selectedItemName;

    private Object selectedItem;

    /**
     * Creates a new instance of DialogChooserManagedBean
     */
    public DialogChooserManagedBean() {
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public DialogChooserListenable getResultListener() {
        return resultListener;
    }

    public void setResultListener(DialogChooserListenable resultListener) {
        this.resultListener = resultListener;
    }

    public String getSelectedItemName() {
        return selectedItemName;
    }

    public void setSelectedItemName(String selectedItemName) {
        this.selectedItemName = selectedItemName;
    }

    public Object getSelectedItem() {
        return selectedItem;
    }

    public void setSelectedItem(Object selectedItem) {
        this.selectedItem = selectedItem;
    }

    public boolean isSelectedItemEqual(Object selectedItem) {
        return (this.selectedItem != null && this.selectedItem.equals(
                selectedItem));
    }

    public void processSelectClickEvent(Object selectedItem) {

        if (isSelectedItemEqual(selectedItem)) {

            this.selectedItem = null;
        } else {
            this.selectedItem = selectedItem;
        }
    }

    public void cancelSelection() {

        selectedItem = null;
        currentPage = 1;

        if (resultListener != null) {
            resultListener.storeResult(selectedItemName, null);
        }
    }

    public void storeSelection() {

        if (selectedItem != null && resultListener != null) {

            resultListener.storeResult(selectedItemName, selectedItem);

            selectedItem = null;
            currentPage = 1;
        }
    }

}
